package net.qhzw.vip.qiyu.util;


import net.qhzw.vip.qiyu.domain.dto.LoginUserDto;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Arrays;
import java.util.List;

public class SecurityUtils {


    public static Long getUserId() {
        LoginUserDto loginUser = getLoginUser();
        return Long.valueOf(loginUser.getUserId());
    }

    public static LoginUserDto getLoginUser() {
        Authentication authentication = getAuthentication();
        String id = (String) authentication.getPrincipal();
        LoginUserDto loginUserDto = new LoginUserDto();
        List<String> strings = Arrays.asList(id.split(","));
        loginUserDto.setUserId(Long.valueOf(strings.get(0)));
        loginUserDto.setShopId(Long.valueOf(strings.get(1)));
        loginUserDto.setUserType(Byte.valueOf(strings.get(2)));
        return loginUserDto;
    }

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }


}
